package com.ctshk.rpc.order.custom.provider;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.NumberConstant;
import com.ctshk.rpc.order.custom.req.order.CustomOrderIdReq;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;

/**
 * 旅客信息 Provider
 * @author 叶冠峰
 * @since 2021-02-24
 */
public class CustomOrderTouristInfoProvider {


    public String selectListBySelective(IPage<CustomOrderIdReq> page, @Param("req") CustomOrderIdReq req) {
        return new SQL() {{
            SELECT("tsoti.id, tsoti.order_id,tsoti.package_id,tsoti.package_name, tsoti.main_data_id, tsoti.tourist_type_name, tsoti.birthday, tsoti.mobile_area_code, tsoti.mobile, tsoti.email, " +
                    "tsoti.cn_surname, tsoti.cn_name,tsoti.cn_fullname, tsoti.en_surname, tsoti.en_name,tsoti.en_fullname, tsoti.is_full_info," +
                    "tsoti.certificate_type, tsoti.certificate_number, tsoti.certificate_valid_date, tsoti.certificate_place, tsoti.remark, " +
                    "tsoti.emergency_contact_name, tsoti.emergency_mobile_area_code, tsoti.emergency_mobile, tsoti.pay_status, tsotq.refund_status AS quit_refund_status, " +
                    "tsoti.total_receivables, tsoti.total_paid_in, tsoti.remaining_receivables ");
            FROM("t_customized_order_tourist_info tsoti");
            LEFT_OUTER_JOIN("( SELECT temp.tourist_id, temp.refund_status FROM t_customized_order_tourist_quit temp ORDER BY temp.gmt_create DESC LIMIT 1 ) tsotq ON tsotq.tourist_id = tsoti.id");
            WHERE("tsoti.order_id=#{req.id}");
            WHERE("tsoti.is_deleted=" + IsDeletedCode.NO.getCode());
        }}.toString();
    }

    public String reciveListBySchedule(@Param("schedule_id")Long scheduleId){
        return new SQL() {{
            SELECT("tsoti.id, tsoti.group_fee,tsoti.collection_fee,tsoti.adjustment_cost,tsoti.other_amount,tsoti.order_id,tsoti.package_id,tsoti.package_name, tsoti.main_data_id, tsoti.tourist_type_name, tsoti.birthday, tsoti.mobile_area_code, tsoti.mobile, tsoti.email, " +
                    "tsoti.cn_surname, tsoti.cn_name,tsoti.cn_fullname, tsoti.en_surname, tsoti.en_name,tsoti.en_fullname,c.order_number, " +
                    "tsoti.certificate_type, tsoti.certificate_number, tsoti.certificate_valid_date, tsoti.certificate_place, tsoti.remark, " +
                    "tsoti.emergency_contact_name, tsoti.emergency_mobile_area_code, tsoti.emergency_mobile, tsoti.pay_status, tsotq.refund_status AS quit_refund_status, " +
                    "tsoti.total_receivables, tsoti.total_paid_in, tsoti.remaining_receivables,c.order_source,c.create_id ");
            FROM("t_customized_order_tourist_info tsoti");
            LEFT_OUTER_JOIN("( SELECT temp.tourist_id, temp.refund_status FROM t_customized_order_tourist_quit temp ORDER BY temp.gmt_create DESC LIMIT 1 ) tsotq ON tsotq.tourist_id = tsoti.id");
            LEFT_OUTER_JOIN("t_customized_order c ON tsoti.order_id = c.id");
            WHERE("tsoti.schedule_id=#{schedule_id}");
            WHERE("tsoti.is_deleted=0");
        }}.toString();
    }

    public String reciveListByTourist(@Param("id")Long id){
        return new SQL() {{
            SELECT("a.id,b.cn_fullname,b.en_fullname,a.collection_number,a.paid_in_amount,a.collection_type,a.collection_way,a.currency_type,a.payer,a.remark,a.`status`,a.reject_msg,a.gmt_create,a.create_id");
            FROM("t_customized_order_collection a");
            LEFT_OUTER_JOIN("t_customized_order_tourist_info b ON a.tourist_id = b.id");
            WHERE("a.tourist_id = #{id} AND a.is_deleted=0" );
        }}.toString();
    }


    public String selectListCollectionBySelective(IPage<CustomOrderIdReq> page, @Param("req") CustomOrderIdReq req) {
        StringBuilder sb = new StringBuilder();
        sb.append("select tsoti.id, tsoti.order_id, tsoti.main_data_id, tsoti.tourist_type_name,tsoti.collection_fee_json, ");
        sb.append("tsoti.cn_surname, tsoti.cn_name, tsoti.en_surname, tsoti.en_name, ");
        sb.append("tsoti.group_fee, tsoti.pay_status, tsotq.refund_status AS quit_refund_status, ");
        sb.append("(SELECT group_concat( CONCAT( a.amount, '])', a.plus_type, '])', a.NAME ) SEPARATOR '})' ) FROM t_customized_order_tourist_fee_adjust a WHERE a.tourist_id = tsoti.id AND a.order_id = #{req.id} AND a.is_deleted = 0) feeAdjustDTOList, ");
        sb.append("(SELECT group_concat( CONCAT( a.amount, '])', a.fee_name) SEPARATOR '})' ) FROM t_customized_order_tourist_other_fee a WHERE a.tourist_id = tsoti.id AND a.order_id = #{req.id} AND a.fee_type = " + NumberConstant.ONE.getValue() +" ) collectionFeeDTOList, ");
        sb.append("(SELECT group_concat( CONCAT( a.amount, '])', a.fee_name) SEPARATOR '})' ) FROM t_customized_order_tourist_other_fee a WHERE a.tourist_id = tsoti.id AND a.order_id = #{req.id} AND a.fee_type = "+ NumberConstant.TWO.getValue() +" ) otherFeeDTOList, ");
        sb.append("tsoti.total_receivables, tsoti.total_paid_in,tsoti.remaining_receivables ");
        sb.append("from t_customized_order_tourist_info tsoti ");
        sb.append("left join ( SELECT temp.tourist_id, temp.refund_status FROM t_customized_order_tourist_quit temp ORDER BY temp.gmt_create DESC LIMIT 1 ) tsotq ON tsotq.tourist_id = tsoti.id ");
        sb.append("where tsoti.order_id=#{req.id} AND tsoti.is_full_info = 1 AND tsoti.is_deleted = 0");
        return sb.toString();
    }

}
